//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2004-2006
// Holux is registered trademark of Holux Technology Inc.
// ALAN and ALAN Map 500 are registered trademarks of ALAN Group.
//
// Holux LST map list parser.
//

# include "StdAfx.h"
# include "HoluxLst.h"
# include "HoluxCrdLoader.h"
# include "Map.h"
# include "Load.h"
//# include "Log.h"
//# include "Status.h"

class CLstLoader : public CMapLoader {
  public:
	// Override.
	virtual bool IsValid (const char * _pData, size_t _cFileSize) const;
	virtual LoadStatus_t Load (const char * _pData, size_t _cFileSize, const char * _strFileName, map_t * _pMap);
};

class CHoluxLstLoader : public CLstLoader {};
class CAlanLstLoader  : public CLstLoader {};

DECLARE_MAP_LOADER (CHoluxLstLoader, "holux-lst", "lst", "Holux MapShow map list (*.lst)");
DECLARE_MAP_LOADER (CAlanLstLoader,  "alan-lst",  "lst", "ALAN Map 500 map list (*.lst)");

bool CLstLoader::IsValid (const char * _pData, size_t _cFileSize) const {
	if (_cFileSize < sizeof (CLstHdr))
		return false;

	const CLstHdr * const pHdr = reinterpret_cast<const CLstHdr *> (_pData);
	CCrdLoader cl;
	return cl.IsValid (_pData + pHdr->maps [0].dwFileOffset, _cFileSize - pHdr->maps [0].dwFileOffset);
}

LoadStatus_t CLstLoader::Load (const char * _pData, size_t _cFileSize, const char * _strFileName, map_t * _pMap) {
	const CLstHdr  * const pHdr  = reinterpret_cast<const CLstHdr *> (_pData);
	const CLstHdr2 * const pHdr2 = reinterpret_cast<const CLstHdr2 *> (pHdr->maps + pHdr->wMaps);
	for (size_t cMap = 0; cMap < pHdr->wMaps; ++ cMap) {
		const CLstMapRef & mr = pHdr->maps [cMap];
		assert (pHdr2->lX0 <= mr.lX0);
		assert (pHdr2->lX1 >= mr.lX1);
		assert (pHdr2->lY0 <= mr.lY0);
		assert (pHdr2->lY1 >= mr.lY1);

		CCrdLoader cl;
		map_t map;
		cl.Load (_pData + mr.dwFileOffset, _cFileSize - mr.dwFileOffset, _strFileName, & map);
		_pMap->Join (map);
	}
	return lsOK;
}